package logic

import (
	"context"
	"errors"
	"shuqi-novel/models/mysql"
	"shuqi-novel/novelrpc/internal/svc"
	"shuqi-novel/novelrpc/novelrpc"
	"shuqi-novel/pkg"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录
func (l *LoginLogic) Login(in *novelrpc.LoginRequest) (*novelrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.FirstUserByUserName(mysql.DB, in.UserName)
	if err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, errors.New("用户未注册")
	}
	if pkg.Md5(in.Password) != user.Password {
		return nil, errors.New("密码错误")
	}
	return &novelrpc.LoginResponse{
		UserId: int64(int(user.ID)),
	}, nil
}
